package bx.job.jobrouter;

import bx.job.jobrouter.impl.JobWorkerRandomRouter;
import com.google.common.collect.Maps;
import lombok.Getter;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Collections;
import java.util.HashMap;

public class JobRouter {
    private static IJobWorkerRouter DEFAULT_WORKER_ROUTER = new JobWorkerRandomRouter();
    private static HashMap<String, IJobWorkerRouter> workerRouterMap;


    static {
        initWorkerRouter();
    }

    public static IJobWorkerRouter getWorkerRouterOrDefault(String routerName) {
        return workerRouterMap.getOrDefault(routerName, DEFAULT_WORKER_ROUTER);
    }

    private static void initWorkerRouter() {
        HashMap<@Nullable String, @Nullable IJobWorkerRouter> map = Maps.newHashMap();
        for (JobRouterEnum routerEnumItem : JobRouterEnum.values()) {
            map.put(routerEnumItem.getRouterName(), routerEnumItem.getRouter());
        }
        workerRouterMap = (HashMap<String, IJobWorkerRouter>) Collections.unmodifiableMap(map);
    }

    enum JobRouterEnum {
        RANDOM("RandomRouter", new JobWorkerRandomRouter());
        @Getter
        private String routerName;
        @Getter
        private IJobWorkerRouter router;

        JobRouterEnum(String routerName, IJobWorkerRouter router) {
            this.routerName = routerName;
            this.router = router;
        }
    }
}
